package com.yehuishou.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yehuishou.pojo.BackAssessment;
import com.yehuishou.pojo.BackItem;
import com.yehuishou.pojo.BackOrder;
import com.yehuishou.pojo.Brand;
import com.yehuishou.pojo.Classify;
import com.yehuishou.pojo.ReceiverAddress;
import com.yehuishou.pojo.User;
import com.yehuishou.service.AssessmentValueService;
import com.yehuishou.service.BackAssessmentService;
import com.yehuishou.service.BackItemService;
import com.yehuishou.service.BackOrderService;
import com.yehuishou.service.BrandService;
import com.yehuishou.service.ClassifyService;
import com.yehuishou.service.EnterprisebackService;
import com.yehuishou.service.ModelService;
import com.yehuishou.service.ReceiverAddressService;
import com.yehuishou.util.StringToDateUtil;

/**
 * 
 * @author xiaosa
 * @className AssessmentController.java
 * @descripe  评估相关操作的控制器
 * @data 2018年6月8日
 */
@Controller
public class AssessmentController {
	
	@Resource
    private BackOrderService backOrderService;
	
	//快递回收收件地址
	@Autowired
	private ReceiverAddressService receiverAddressService;
	
	@Autowired
	private BrandService BrandService;
	
	@Autowired
	private EnterprisebackService enterprisebackService;
	
	@Autowired
	private ModelService modelService;
	
	@Autowired
	private ClassifyService classifyService;
	
	@Autowired
	private BackItemService backItemService;
	
	@Autowired
	private BackAssessmentService backAssessmentService;
	
	@Autowired 
	private AssessmentValueService assessmentValueService;
	
	
	Map<String, Object> map = new HashMap<>();
	//进去评估页面的数据拿取
	
	
   //跳到品牌详情页面
	@RequestMapping("tobrandinfo")
	public String  tobrandinfo(String npage,String cid,String pid,HttpServletRequest req,String brandname) {
	//获取所有的品牌
		
		List<Classify> classifylist =classifyService.getAllClassify();
	
		Integer ncid =0;
		
		String classifyname="";
		if(cid!=null&&cid!="") {
		ncid = Integer.parseInt(cid);
		
		classifyname =classifyService.getClassifyBycid(ncid).getClassifyname();
		}
		
		List<Brand> brandlist= new ArrayList<>();
		
		if(ncid<1) {
			
			brandlist =	BrandService.getBycidAll(1);
		}else {
			
		    brandlist =	BrandService.getBycidAll(ncid);
		}
	Integer page=1;
	if(npage!=null&&npage!="") {
		
		page = Integer.parseInt(npage);
	}
	
	
	Integer pids=0;
	if(pid!=null&&pid!="") {
		
		pids = Integer.parseInt(pid);
	}
	
	PageHelper.startPage(page, 24);
	List<BackItem> backItems =backItemService.getBackItem(brandname,pids,ncid);
	PageInfo<BackItem>  pageinfo = new PageInfo<>(backItems);
	req.setAttribute("pageinfo", pageinfo);
	req.setAttribute("classifylist", classifylist);
	req.setAttribute("brandlist", brandlist);
	req.setAttribute("cid", cid);
	req.setAttribute("pid", pid);
	req.setAttribute("brandname",brandname);
	req.setAttribute("classifyname", classifyname);
	return "b52";
}
	
	@ResponseBody
	@RequestMapping("/tosearchPage")
	public Map<String, Object>  tosearchPage(String brandname,String npage,String cid,String pid,HttpServletRequest req) {
	//获取所有的品牌
		List<Classify> classifylist =classifyService.getAllClassify();
	
	Integer page=1;
	if(npage!=null&&npage!="") {
		
		page = Integer.parseInt(npage);
	}
	
	Integer ncid =0;
	
     if(cid!=null&&cid!="") {
		
		ncid = Integer.parseInt(cid);
	}
	
	
	List<Brand> brandlist= new ArrayList<>();
	
	if(ncid<1) {
		
		brandlist =	BrandService.getBycidAll(1);
	}else {
		
	    brandlist =	BrandService.getBycidAll(ncid);
	}
	
	Integer pids=0;
	if(pid!=null&&pid!="") {
		
		pids = Integer.parseInt(pid);
	}
	
	
	PageHelper.startPage(page, 24);
	List<BackItem> backItems =backItemService.getBackItem(brandname,pids,ncid);
	PageInfo<BackItem>  pageinfo = new PageInfo<>(backItems);
	map.put("pageinfo", pageinfo);
	map.put("cid", cid);
	map.put("pid", pid);
	return map;
}
	
	

	@ResponseBody
	@RequestMapping("/getAssessmentValue")
	public Map<String, Object>  getAssessmentValue(Integer id){
		
		List<BackAssessment> as =new ArrayList<>();
		as =backAssessmentService.selectBackAssessment(id);
		map.put("assessment", as);
		
		return map;
		
	}

	//跳转到商品评估的页面面
	@RequestMapping("/toAssessValue")
	public String toAssessmentValue(Integer id,HttpServletRequest req){
		
		req.setAttribute("backAessess",backAssessmentService.selectBackAssessment(id));
		req.setAttribute("backas", backItemService.getById(id));
		
		return "recycling";
		
	}
	

//根据用户的选择计算 评估的结果
	@RequestMapping("/getAssessmentPrice")
	public String getAsValueResult(String back_valids,HttpServletRequest req,HttpSession session,
			Model model,Integer sreceiverid){
		User user = (User) session.getAttribute("user");
		if(user!=null){
			
		String phone=user.getPhone();
		req.setAttribute("phone", phone);
		}
		 List<BackOrder> l=backOrderService.selectStore();
		 System.out.println("llll"+l);
		 req.setAttribute("l", l);
		String arr [] = back_valids.split(";");
		
		if(arr!=null&&arr.length>1) {
		if(arr.length>1) {
		map  = assessmentValueService.getAssessmentValueByids(arr);
		req.setAttribute("as_info", map);
		req.setAttribute("back_valids", back_valids);
		
		ReceiverAddress rd = receiverAddressService.findAll(sreceiverid);
		model.addAttribute("rd",rd);  	  //数据
		 }
		}
		return "inquiry";
		
	}
	
	
	@ResponseBody
	@RequestMapping("/getBackvalue")
	public Map<String, Object>  getBackvalue(String [] vals){
	    
		 //根据id 
		StringBuffer buff = new StringBuffer();
		if(vals!=null&&vals.length>0) {
		for (String i : vals) {
             buff.append(";"+i);
		}
	}
		//属性值得id
		String backassess=buff.toString();
		map.put("back_valids", backassess);
		return map;
	}

	@RequestMapping("/toEnterpriseback")
	public String toenterpriseback(HttpServletRequest req) {
		HttpSession session=req.getSession();
		session.setAttribute("state",UUID.randomUUID().toString().substring(0, 5) );
		User user =(User)session.getAttribute("user");
		req.setAttribute("user", user);
		return "qiyehuishou";
	
	
	}

	//提交企业回收信息
	
	@RequestMapping("/epback_sb")
	public String enterpriseback(
			String companyname,
			String clientname,
			String descripinfo,
			String phone,
			HttpSession session
			) {
		Object o=session.getAttribute("state");
		User user =(User)session.getAttribute("user");
		Integer uid = user.getUid();
		if(o!=null) {
		enterprisebackService.insertEnterpriseback(companyname, clientname, phone, descripinfo, new Date(),uid);
		session.removeAttribute("state");
		}
		return "ep_success";
	}
	
	
}	
